package cn.fl.atc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.atc.service.IAtcBscInfoLService;
import cn.fl.atc.transform.AtcBscInfoLVOTransform;
import cn.fl.atc.vo.AtcBscInfoLVO;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUlFilesRecVO;
import cn.fl.system.vo.SysUsrVO;
import fl.spl.facade.atc.api.IAtcBscInfoLApiService;
import fl.spl.facade.atc.condition.AtcBscInfoLQueryCondition;
import fl.spl.facade.atc.constant.AtcBscInfoConstant;
import fl.spl.facade.atc.dto.AtcBscInfoLDTO;

/**
 * @Description: 汽贸城信息日志表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AtcBscInfoLServiceImpl implements IAtcBscInfoLService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AtcBscInfoLServiceImpl.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAtcBscInfoLApiService atcBscInfoLApiService;
	@Autowired
    private ISysUlFilesRecService sysUlFilesRecService;
	@Autowired
	private ISysUsrService sysUsrService;
	@Override
	public AtcBscInfoLVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
		}
		return null;
	}

	@Override
	public Long add(AtcBscInfoLVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.atcBscInfoLApiService.add(AtcBscInfoLVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.atcBscInfoLApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AtcBscInfoLVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.atcBscInfoLApiService.modify(AtcBscInfoLVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.atcBscInfoLApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	@Override
	public List<SysUlFilesRecVO> searchList(AtcBscInfoLQueryCondition qc) {
		List<AtcBscInfoLDTO> searchAtcBscInfoLList = atcBscInfoLApiService.searchAtcBscInfoLList(qc);
		List<AtcBscInfoLVO> voList = AtcBscInfoLVOTransform.toVOList(searchAtcBscInfoLList);
		List<SysUlFilesRecVO> list = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(voList)){
			List<String> pkIdList = new ArrayList<>();
			Map<String,AtcBscInfoLVO> map = new HashMap<>();
			for (AtcBscInfoLVO vo : voList) {
				pkIdList.add(String.valueOf(vo.getId()));
				map.put(String.valueOf(vo.getId()), vo);
			}
			SysUlFilesRecQueryCondition fileQc = new SysUlFilesRecQueryCondition();
			fileQc.setTabNm(AtcBscInfoConstant.UPLOAD_TAB_NM_L);
			fileQc.setPkIdList(pkIdList);
			fileQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			list = this.sysUlFilesRecService.searchList(fileQc);
			if (CollectionUtils.isNotEmpty(list)) {
	            for (SysUlFilesRecVO sysUlFilesRecVO : list) {
	            	//获取真实名字
		            SysUsrVO usrVO = this.sysUsrService.selectById(sysUlFilesRecVO.getCrtUsrId(), CurrentThreadContext.getCurrentSysCd());
	            	if(null != usrVO){
	            		sysUlFilesRecVO.setCrtUsrRlNm(usrVO.getRefRlNm());
	            	}
		            if(map.containsKey(sysUlFilesRecVO.getPkId())){
	            		AtcBscInfoLVO atcBscInfoLVO = map.get(sysUlFilesRecVO.getPkId());
	            		sysUlFilesRecVO.setBeginDt(atcBscInfoLVO.getBeginDt());
	            		sysUlFilesRecVO.setEndDt(atcBscInfoLVO.getEndDt());
	            	}
	            	
				}
	        }
		}
		return list;
	}
}
